﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.BusinessData.MetadataModel;

namespace MsExchangeBcsConnector.ExchangeBdcModel.Helpers
{
    internal static class UserProfileHelper
    {

        /// <summary>
        /// Return list of mailboxes for all users 
        /// TODO: we might need append filter for Exchange domain
        /// </summary>
        /// <param name="ProfileManager"></param>
        /// <returns></returns>
        internal static List<MailBox> LoadMailBoxList(UserProfileManager ProfileManager)
        {
           List<MailBox> entityList = new List<MailBox>();

           foreach (UserProfile profile in ProfileManager)            
           { 
             //   UserProfile profile = ProfileManager.GetUserProfile("makhlu");

                MailBox mailbox = UserProfileHelper.LoadMailBoxFromProfile(profile);

                if (mailbox != null)
                { // If mailbox has an address

                    MailBox existingMailBox = entityList.FirstOrDefault<MailBox>(m => m.EMail.Equals(mailbox.EMail));

                    if (existingMailBox == null)
                    {
                        entityList.Add(mailbox);     /// if this is a new mailbox - append it
                    }
                    else
                    {
                        // append login
                        existingMailBox.AddLogin(mailbox.Login);
                    }
               }
           }

            return entityList;
        }

        /// <summary>
        /// Read mailbox data from UserProfileManager
        /// </summary>
        /// <param name="ProfileManager"></param>
        /// <param name="email">MailBox email address</param>
        /// <returns></returns>
        internal static MailBox LoadMailBox(UserProfileManager ProfileManager, string email)
        {

            ProfileBase[] matchingProfiles = ProfileManager.Search(email);

            if (matchingProfiles != null && matchingProfiles.Length > 0)
            {
                MailBox mailbox = UserProfileHelper.LoadMailBoxFromProfile((UserProfile)matchingProfiles[0]);
                
                for (int i = 0; i < matchingProfiles.Length; i++)
                {

                    MailBox tempMailBox = UserProfileHelper.LoadMailBoxFromProfile((UserProfile)matchingProfiles[i]);
                    if (tempMailBox != null)
                        mailbox.AddLogin(tempMailBox.Login);
                }

                return mailbox;
            }
            return null;
        }   

       private static MailBox LoadMailBoxFromProfile(UserProfile profile)
        {
            MailBox mailbox = new MailBox();
            if (profile[PropertyConstants.WorkEmail] != null
                        && profile[PropertyConstants.WorkEmail].Value != null)
                mailbox.EMail = profile[PropertyConstants.WorkEmail].Value.ToString();

            if (profile[PropertyConstants.AccountName] != null
                        && profile[PropertyConstants.AccountName].Value != null)
                mailbox.Login = profile[PropertyConstants.AccountName].Value.ToString();

            if (!string.IsNullOrEmpty(mailbox.EMail) && !string.IsNullOrEmpty(mailbox.Login))
                return mailbox;
            else
                return null;

        }

    }
}
